﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevTool.Command;
namespace DevTool.Contraner
{
    public partial class httpControl : UserControl
    {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        public httpControl()
        {
            InitializeComponent();
            cb_type.SelectedIndex = 0;
            cb_subtype.SelectedIndex = 0;
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar==13)
            {
                var keys = this.textBox1.Text.Trim();
                var values = this.textBox2.Text.Trim();
                dic.Add(keys,values);
                this.textBox1.Focus();
                this.richTextBox_parm.AppendText($"key:{keys}  value:{values} \r\n");
                this.textBox1.Text = string.Empty;
                this.textBox2.Text = string.Empty;
                this.textBox1.Focus();

            }
        }

        private void bt_ToJSON_Click(object sender, EventArgs e)
        {
            if (dic.Count>0)
            {
                this.richTextBox_parm.AppendText(dic.ToJsonString()+"\r\n");
             
            }
          
        }

        private void bt_clear_Click(object sender, EventArgs e)
        {
            this.richTextBox_parm.Clear();
            dic.Clear();
        }

        private void bt_OK_Click(object sender, EventArgs e)
        {
            var result = string.Empty;
            var url = cb_url.Text.Trim();
            var types = cb_type.Text.Trim();
            var subtype = cb_subtype.Text.Trim();
            if (!string.IsNullOrEmpty(url))
            {
                try
                {
                    if (subtype.ToLower() == "application/json")
                    {
                        HttpSendResult.Token = this.txt_Token.Text.Trim();
                        if (dic.Count>0)
                        {
                            result = HttpSendResult.SendRequest(url, dic.ToJsonString(), types);
                        }
                        else
                        {
                            var jsonstr = richTextBox_parm.Text.Trim();
                            result = HttpSendResult.SendRequest(url, jsonstr, types);
                        }
                        
                    }
                    else
                    {
                        //GET  POST
                        if (types.ToUpper()== "GET")
                        {
                            HttpSendResult.Token = this.txt_Token.Text.Trim();
                            if (dic.Count>0)
                            {
                                result = HttpSendResult.Get(url, dic);
                            }
                            else
                            {
                                result = HttpSendResult.Get(url,"");
                            }
                           
                        }
                        else
                        {
                            HttpSendResult.Token = this.txt_Token.Text.Trim();
                            if (dic.Count > 0)
                            {
                                result = HttpSendResult.Post(url, dic);
                            }
                            else
                            {
                                result = HttpSendResult.Post(url);
                            }
                           
                        }
                       
                    }
                    this.richTextBox_context.Text=result;

                }
                catch (Exception ex)
                {
                    this.richTextBox_context.AppendText(ex.Message+"\r\n");
                }
               
            }
            
        }

        private void bt_comtxt_clear_Click(object sender, EventArgs e)
        {
            this.richTextBox_context.Clear();
        }

        private void bt_json_Click(object sender, EventArgs e)
        {
            var json= this.richTextBox_context.Text;
            if (!string.IsNullOrEmpty(json))
            {
                this.richTextBox_context.Text = json.ConvertJsonString();
            }
           
        }
    }
}
